#!/bin/sh
#chkconfig: 2345 80 05
#description: admin-server

#jdk
export JAVA_HOME=/usr/local/java/jdk1.8.0_192
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
source /etc/profile

#根目录
DIR=/home/daison/encrypt/task

#启动类
SERVER_NAME=daison-beacon-task
JAR_HOME=$DIR/$SERVER_NAME-1.0.1-SNAPSHOT.jar
if [ ! -f "$JAR_HOME" ];then
   echo "$JAR_HOME is not exist!"
   exit 1
fi
echo "start class jar dir: $JAR_HOME"

#日志目录
LOGS_DIR=/home/daison/encrypt/logs
#if [ ! -d ${path} ];then
if [ -d ${LOGS_DIR} ];then
    echo dir ${LOGS_DIR} exist!
else
    sudo mkdir $LOGS_DIR
    sudo chmod 777 -R $LOGS_DIR
fi
#log file 
LOG_NAME=$LOGS_DIR/$SERVER_NAME.log
sudo echo "">$LOG_NAME
echo "log file : $LOG_NAME"

#监控服务目录
SKYWALKING_HOME=/home/daison/skywalking/skywalking-6.5
echo "skywalking monitor server dir : $SKYWALKING_HOME"

#java agent jar
JAVA_AGENT=-javaagent:$SKYWALKING_HOME/agent/skywalking-agent.jar
echo "javaagent cmd : $JAVA_AGENT"

#service name
SERVICE_NAME=-Dskywalking.agent.service_name=$SERVER_NAME
echo "service name : $SERVICE_NAME"

#backend service
BACKEND_SERVICE=-Dskywalking.collector.backend_service=8.135.42.12:11800
echo "backend service : $BACKEND_SERVICE"


#启动服务
#nohup java -jar $JAR_HOME -javaagent:$SKYWALKING_HOME/agent/skywalking-agent.jar -Dskywalking.agent.service_name=daison-websocket -Dskywalking.collector.backend_service=localhost:11800 > $DIR/logs/admin.log 2>&1 &

#nohup java -jar $JAR_HOME $JAVA_AGENT $SERVICE_NAME -Dskywalking.collector.backend_service=localhost:11800 > $LOG_NAME 2>&1 &
#nohup java $JAVA_AGENT $SERVICE_NAME $BACKEND_SERVICE -jar $JAR_HOME > $LOG_NAME 2>&1 &


case "$1" in
	start)
		${0} stop
		ps -ef | grep $SERVER_NAME | grep -v grep
		if [ $? -eq 0 ];then
		    echo "server is running......."   
		else
		    echo "start server $SERVER_NAME"
		    #nohup java $JAVA_AGENT $SERVICE_NAME $BACKEND_SERVICE -jar -Xms512m -Xmx1024m $JAR_HOME > $LOG_NAME 2>&1 &
	            $DIR/xjar java -server -Xms512m -Xmx1024m -jar $JAR_HOME > $LOG_NAME 2>&1 &
		    echo "staring server success !"
		    ps -ef | grep $SERVER_NAME | grep -v grep
		fi
		;;
	stop)
		echo "stop server $SERVER_NAME ......."
		param=`ps -ef | grep $SERVER_NAME | grep -v grep | awk  '{print $2}'`
		if [ "$param" = "" ];then
		   echo "servr $SERVER_NAME not running"
		   exit 1
		fi
		
		for item in ${param[*]}
		do
		   echo "stop process num: $item"
		   sudo kill -9 $item
		   echo " stop $SERVER_NAME success"
		done
		;;	
	status)
		param=`ps -ef | grep $SERVER_NAME | grep -v grep`
		if [ "$param" = "" ];then
		   echo "servr $SERVER_NAME not running"
		   exit 1
		else
		   echo "server $SERVER_NAME is running "
		   echo $param
		fi
		;;
	restart)
		${0} stop
		${0} start
		;;
	  *)
		echo "usage: /home/daison/admin/admin-server.sh {start|stop|restart}" >&2
		exit 1
esac


